var app = getApp()
var util = require('../utils/util.js')

//时间转换格式,将秒数转为 xx:xx:xx 形式
function formatTime(time) {
  if (time <= 0) return "00:00:00"
  var day = parseInt(time / 3600 / 24)
  var h = parseInt(time % (3600 * 24) / 3600)
  var m = parseInt(time % 3600 / 60)
  var s = time % 60
  if (day > 0) return day + "天 " + [h, m, s].map(util.formatNumber).join(":")
  else return [h, m, s].map(util.formatNumber).join(":")
}
//倒计时
function countDown(time, cb) {
  //格式化时间,首次展示
  cb(formatTime(time))
  //每隔一分钟刷新一次
  var interval = setInterval(function() {
    cb(formatTime(time))
    if (time <= 0) clearInterval(interval)
    time = time - 1
  }, 1000)
  return interval
}

function getTimeNode(time) {
  var date = new Date(time)
  var node = {
    year: date.getFullYear(),
    month: date.getMonth() + 1,
    day: date.getDate(),
  }
  return node
}

function getDateList() {
  var date = new Date()
  var time = date.getTime()
  var list = []
  for (var i = 0; i < 14; i++) {
    var node = getTimeNode(time)
    list.push(node)
    time += 24 * 3600 * 1000
  }
}

module.exports = {
  formatTime:formatTime,
  countDown: countDown,
}